<?php 
class GoodsModel extends ViewModel{
	public $table = 'goods';
	//关联栏目表和商品表
	public $view=array(
		'category'=>array(
			'type'=>INNER_JOIN,
			'on'=>'goods.cid=category.cid'
			) 
		);
	//验证
	public $validate=array(
		array('gname','nonull','商品名不能为空',2,3),
		array('cid','nonull','栏目不能为空',2,3),
		array('stock','nonull','库存不能为空',2,3)
		// array('spec[stock]','nonull','库存不能为空',2,3), 

		);
	//自动完成
	public $auto = array(
		array('flag','flagAuto','method',1,3),
		array('addtime','strtotime','function',1,3),
		array('index_pic','indexPicAuto','method',2,3),
		array('list_pic','listPicAuto','method',2,3)
		);
	public function flagAuto($v){
		return implode(",", $v);
	}
	public function indexPicAuto($v){
		if(!empty($_FILES['index_pic']['name'])){
			$upload=new Upload();
			$info=$upload->upload('index_pic');
			return $info[0]['path'];

		}
	}
	public function listPicAuto($v){
		if(!empty($_FILES['list_pic']['name'])){
			$upload=new Upload();
			$info=$upload->upload('list_pic');
			return $info[0]['path'];
		}
	}
	//添加商品
	public function addGoods(){
		
		if($this->create()){
			// p($_POST);exit;
			$cid=Q('cid',0,'intval');
			// p($cid);exit;
			//品牌id
			$bid=Q('bid',0,'intval');
			
			$gid=$this->add();
			//规格的商品数剧
			$specData=$_POST['spec'];
			
			//添加商品id 到品牌
			if($bid){
				$this->addbrand($gid,$bid);
			}
			if(!empty($_POST['spec'])){
				$attr=$this->addStock($gid,$specData);
			// p($attr);exit;
				$this->addStockAttr($cid,$gid,$attr); 
			}
			if($gid){
				$this->upld($gid);
				$this->saveGoodsAttr($gid);
				return $gid; 
			}else{
				$this->error;
				return false;
			}
			
			
		}	
	}
	
	// cid gid stid avid 库存属性关联表
	public function addStockAttr($cid,$gid,$attr){
		$arr=array();
		
		foreach ($attr['stid'] as $key => $value) {
			foreach ($attr['avid'] as $k => $v) {
				if(strstr($v[$key],'|')){
					$info=explode('|',$v[$key]);
					// p($info);
					$avid=$info[0];
				}
				$arr['avid']=$avid;
				$arr['stid']=$value;
				$arr['cid']=$cid;
				$arr['gid']=$gid;
				M('stock_attr')->add($arr);
			}
			
		}
		// p($attr);exit;
	}
	//库存表
	public function addStock($gid,$specData){
		$attr=array();
		// p($specData);exit;
		foreach ($specData ['goods_sn'] as $key => $value) {
			$array=array('gid'=>$gid,'goods_sn'=>$value,'price'=>$specData['price'][$key],'stock'=>$specData['stock'][$key] );
			$stid=M('stock')->add($array);
			$avid=$specData['attr'];
			$attr['avid']=$avid;
			$attr['stid'][]=$stid;
		}
		return $attr;
	}
	//添加商品属性
	public function saveGoodsAttr($gid){
		
		$Model=M('goods_attr');
		$cid=Q('cid',0,'intval');
		
		$data=$_POST['spec']['attr'];
		// 是规格的属性值
		$arr=array();

		foreach ($data as $k => $v) {
			$v=array_unique($v);
			foreach ($v as $b => $d) {
				if(strstr($d,'|')){
					$info=explode('|',$d);
					$arr['avid']=$info[0];
					$arr['cid']=$cid;
					$arr['value']=$info[1];
					$arr['gid']=$gid;
				}	
				$Model->add($arr);
			}
		}
	//不是规格的属性值添加
	if(empty($_POST['attr']))return ;
	$attr=$_POST['attr'];
		foreach($attr as $key=>$v){
			if(strstr($key, "|")){
				$info=explode("|", $key);
				$avid=$info[1];
				$attr_value=$v;
			}else{
				$info=explode('|',$v);
				$avid=$info[0];
				$attr_value=$info[1];
			}
			$Model->add(array('value'=>$attr_value,'cid'=>$cid,'gid'=>$gid,'avid'=>$avid));
		}
	}
	//图片处理
	public function upld($gid){
				$upload=new Upload();
				$info=$upload->upload('pic');
				$image=new Image();
				foreach ($info as $pic) {
					$bigName=$pic['dir'].$pic['filename']."_800X800.".$pic['ext'];
					$mediumName=$pic['dir'].$pic['filename']."_350X350.".$pic['ext'];
					$smallName=$pic['dir'].$pic['filename']."_50X50.".$pic['ext'];
					$bigPic=$image->thumb($pic['path'],$bigName,800,800);
					$mediumPic=$image->thumb($pic['path'],$mediumName,350,350);
					$smallPic=$image->thumb($pic['path'],$smallName,50,50);

					$addPic=M('goods_pic')->add(
						array(
							'big'=>$bigPic,
							'medium'=>$mediumPic,
							'small'=>$smallPic,
							'gid'=>$gid
							)
						);
					if(is_file($pic['path'])){
						unlink($pic['path']);
					}
				}
	}


	//商品编辑
	public function editGoods(){
	
		if($this->create()){
			unset($this->data['index_pic']);
			unset($this->data['list_pic']);
			$gid=Q('gid',0,'intval');

			$this->upld($gid);

			$state=$this->save();
			if($state){
				M('goods_attr')->del(array('gid'=>$gid));
				M('stock_attr')->del(array('gid'=>$gid));
				$this->saveGoodsAttr($gid);
				$this->saveStock($gid);
				return $state;
			}
		}
		return false;
	}

	//添加商品属性
	public function saveStock($gid){
		// p($_POST);exit;
		$stock =$_POST['spec'];

		$stockModel=M('stock');
		//删除旧的库存数据
		$stockModel->where("gid=$gid")->del();

		$goodsAttr=array();
		foreach ($stock['price'] as $i => $price) {
			$data=array();
			if(empty($stock['stock'][$i]) || empty($stock['price'][$i]) ||  empty($stock['goods_sn'][$i]))continue;
			if(isset($stock['stid'][$i])){
				$data['stid']=$stock['stid'][$i];
			}
			$data['stock']=$stock['stock'][$i];
			$data['price']=$stock['price'][$i];
			$data['goods_sn']=$stock['goods_sn'][$i];
			$data['gid']=$gid;
			//添加数据到stock
			$stid=$stockModel->add($data);
			if($stid){
				$stockAttrModel=M('stock_attr');
				foreach ($stock['attr'] as $attr) {
					$specData=array();
					$info=explode('|',$attr[$i]);
					$specData['avid']=$info[0];
					$specData['gid']=$gid;
					$specData['stid']=$stid;
					$specData['cid']=Q('cid');
					$stockAttrModel->add($specData);
					$goodsAttr[$info[0]]=array('cid'=>Q('cid'),'gid'=>$gid,'avid'=>$info[0],'value'=>$info[1]);
					
				}
			}
		}
		//添加数据到good_attr
		foreach ($goodsAttr as $attr) {
			// p($attr);exit;
			M('goods_attr')->add($attr);
		}
		$data['price']=M('stock')->where("gid=$gid")->order('price ASC')->getField('price');
		$data['stock']=M('stock')->where("gid=$gid")->sum('stock');
		M('goods')->where("gid=$gid")->save($data);

	}

	//品牌logo图像处理
	public function logo($d){

			$upload=new Upload();
			$info=$upload->upload('logo');
			// p($info);exit;
			$image=new Image();
			$logoPath=$info[0]['dir'].$info[0]['filename']."_logo.".$info[0]['ext'];
			$mediumPic=$image->thumb($info[0]['path'],$logoPath,250,70);
			$addPic=M('brand')->where(array('bid'=>$d))->update(array('logo'=>$logoPath));
			if(is_file($info[0]['path'])){
				unlink($info[0]['path']);
			}	
	}
	//添加商品id 到品牌
	public function addbrand($gid,$bid){
		$Model=M('brand')->where(array('bid'=>$bid))->save(array('gid'=>$gid));
		return $Model;
	}

	//删除商品
	public function drop(){
		$gid=Q('gid',0,'intval');
		if($gid){

			$pic=$this->find("gid=$gid");
		//删除内容页的图片
			$img=M('goods_pic')->find("gid=$gid");

			if(is_file($pic['index_pic'])){
				unlink($pic['index_pic']);
			}

			if(is_file($pic['list_pic'])){
				unlink($pic['list_pic']);
			}
			if(is_file($img['big'])){
				unlink($img['big']);
			}
			if(is_file($img['medium'])){
				unlink($img['medium']);
			}
			if(is_file($img['small'])){
				unlink($img['small']);	
			}
			$this->del("gid=$gid");
				M('goods_attr')->del("gid=$gid");
				M('brand')->del("gid=$gid");
				//================删除图片和文件中图片
				M('goods_pic')->del("gid=$gid");
				M('brand')->del("gid=$gid");
				return true;
		}else{
			$this->error('删除失败');
		}

	}

















}




 ?>